'use strict'

let models = require('../models');
let teacherService = require('../services/teacher');
let checkTimeSetService = require('../services/checktimeset');
let attendanceService = require('../services/attendance');

let viewIndex = {
    login: true,
    roles: [ 'grade_manager' ],
    handler(req, res) {
        let user = req.session.login.user;
        let gradeManager = teacherService.getTeacherByUserId(user.id);

        res.render('leader_today_attend_grade-manager', {
            basePath: '',
            gradeManager: gradeManager,
            user: gradeManager
        });
    }
};

let viewTodayAttendance = {
    login: true,
    roles: [ 'grade_manager' ],
    handler(req, res) {
        let user = req.session.login.user;
        let gradeManager = teacherService.getTeacherByUserId(user.id);
        let grade = gradeManager.managedGrade;

        grade.statistics = attendanceService.emptyStatistics();
        grade.natureClasses.forEach(natureClass => {
            natureClass.statistics = attendanceService.getStatisticsOfNatureClassToday(natureClass)
            attendanceService.combineStatistics(grade.statistics, natureClass.statistics);
        });

        let peopleCount = grade.natureClasses.reduce((sum, natureClass) => {
            return sum + natureClass.students.length
        }, 0);

        console.log(grade.statistics);

        res.render('leader_today_attend_grade-manager', {
            basePath: '',
            gradeManager: gradeManager,
            user: gradeManager,
            grade: grade,
            peopleCount: peopleCount,
            AttendanceDescriptions: models.AttendanceDescriptions
        });
    }
};

let viewSearchAttendance = {
    login: true,
    roles: [ 'grade_manager' ],
    handler(req, res) {
        let user = req.session.login.user;
        let gradeManager = teacherService.getTeacherByUserId(user.id);
        let grade = gradeManager.managedGrade;

        let startTime = new Date(Number(req.query.start));
        let endTime = new Date(Number(req.query.end));
        let natureClassId = req.query.nature_class;

        let classes = [];
        if ( req.query.start && req.query.end ) {
            classes = grade.natureClasses;
        }
        if ( natureClassId ) {
            classes = classes.filter(natureClass => natureClass.id == natureClassId);
        }

        classes.forEach(natureClass => {
            natureClass.statisticsList = 
                attendanceService.getStatisticsOfNatureClass(natureClass, startTime, endTime);
            natureClass.totalStatistics = attendanceService.emptyStatistics();
            natureClass.statisticsList.forEach(statistics => {
                attendanceService.combineStatistics(natureClass.totalStatistics, statistics);
            });
        });

        res.render('leader_attend_search', {
            leader: gradeManager,
            user: gradeManager,
            grades: [ grade ],
            classes: classes,
            roleName: 'grade_manager',
            basePath: ''
        });
    }
};

module.exports = {
    viewIndex,
    viewTodayAttendance,
    viewSearchAttendance
};
